<template>
  <div>
    <div class="head">
      <span></span>
      <span>登录</span>
      <span>&emsp;</span>
    </div>

    <div class="body">
      <div class="change">
        <span>&emsp;</span>
        <span>注册</span>
      </div>
      <h4>密码登录</h4>
      <van-form
        @submit="onSubmit"
        :show-error-message="false"
        :show-error="false"
      >
        <van-field
          v-model="tel"
          name="phone"
          placeholder="请输入手机号码"
          :rules="[
            { required: true, message: '手机号码必须填写' },
            { validator: validatorPhone, message: '手机号码不合法' },
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          placeholder="请输入密码"
          :rules="[
            { required: true, message: '请填写密码' },
            { validator: validatorPassword, message: '密码不是一个合法的' },
          ]"
        />
        <div class="other">
          <span></span>
          <span class="forget">忘记密码</span>
        </div>
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit" class="btn"
            >提交</van-button
          >
        </div>
        <p @click="onClick">免密登录</p>
      </van-form>
    </div>

    <div class="bottom">
      <span class="text"
        >————&ensp;&ensp;或通过以下方式登录&ensp;&ensp;————</span
      >
      <div class="pic">
        <span v-for="item in pic" :key="item.index" class="pics">
          <img :src="item" alt="" />
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import loginMixin from "@/mixins/loginMixin";
export default {
  data() {
    return {
      tel: "",
      password: "",
      pic: [
        require("../../img/qq.png"),
        require("../../img/vx.png"),
        require("../../img/wb.png"),
      ],
    };
  },
  mixins: [loginMixin],
  methods: {
    onClick() {
    },
    async onSubmit(formData) {
      console.log(111);
      let ret = await this.$store.dispatch("user/setUserAction", formData);
      if (ret) {
        // 登录成功，token已经存储在vuex中
        // 先成功提示，再跳转
        this.$toast({
          type: "success",
          duration: 1500,
          message: "登录成功",
          // 遮罩层是否显示，为了防抖
          overlay: true,
          // 禁用背景点击
          forbidClick: true,
          onClose: () => {
            // 跳转
            this.$router.push("/home").catch(() => {});
          },
        });
      } else {
        this.$toast({
          type: "fail",
          duration: 1500,
          message: "账号或密码不正确",
          // 遮罩层是否显示，为了防抖
          overlay: true,
          // 禁用背景点击
          forbidClick: true,
        });
      }
    },
  },
  mounted() {
    document
      .querySelector("body")
      .setAttribute("style", "background: linear-gradient(#913cfb, #676de8);");
  },
  beforeDestroy() {
    document.querySelector("body").removeAttribute("style");
  },
};
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
}

.head {
  height: 60px;
  background: white;
  line-height: 60px;
  font-weight: bold;
  font-size: 20px;
  padding-left: 20px;
  display: flex;
  justify-content: space-between;
}

.body {
  color: white;
  .change {
    display: flex;
    justify-content: space-between;
    margin-right: 25px;
    padding-top: 35px;
    span {
      font-size: 16px;
    }
  }
  h4 {
    text-align: center;
    margin: 25px;
  }
  .van-form {
    width: 90%;
    margin: 0 auto;
    height: 250px;
    background-color: white;
    border-radius: 5%;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    .van-field {
      margin: 10px auto;
      width: 90%;
      height: 30px;
    }
    .btn {
      background-color: #b3b7f4;
      border: none;
      margin-top: -25px;
    }
    p {
      color: #666ee8;
      font-size: 14px;
      margin: 0 auto;
      margin-top: -10px;
    }
    .other {
      height: 20px;
      display: flex;
      justify-content: space-between;
      .forget {
        color: #ccc;
        font-size: 12px;
        margin-right: 25px;
      }
    }
  }
}

.bottom {
  position: fixed;
  bottom: 85px;
  margin-left: 75px;

  .text {
    color: white;
    font-size: 12px;
  }
  .pic {
    margin-left: 30px;
    margin-top: 10px;

    img {
      height: 40px;
      width: 40px;
      margin-left: 10px;
    }
  }
}
</style>